﻿using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
  public  class Folders_FilesDAL
    {
        private string DbName;
        public Folders_FilesDAL()
        {
            DbName = "LdtBatchIntelligence";
        }

        /// <summary>
        /// 获取案例数据中间展示内容
        /// </summary>
        public DataTable GetContent(SYS_ArgumentTextRel sa)
        {
            string strSql = string.Format(@" select  SDP_Content from [Ldt_JudgeSource].dbo.SD_CommonDoc_Paragraph where SD_ID in (select SD_ID
from Folders_Files as Files  
join TestCase as TCase ON Files.TCaseID = TCase.TCaseID 
join LogicalParagraph as logp on logp.FL_ID=Files.FL_ID
where TCase.CaseType='{0}'and TCase.TCaseCause='{1}' and Files.FL_BookTypeName='{2}'and logp.LP_Code='{3}' 
and TCase.TCaseCode='{4}') and SDP_ParagraphCode='{3}'  ",sa.ATR_CaseType,sa.CauseId,sa.ATR_BookType,sa.ATR_ParagraphCode,sa.CaseCode,sa.ATR_ParagraphCode);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取裁判理由内容
        /// </summary>
        /// <param name="sa"></param>
        /// <returns></returns>
        public DataTable GetContentCPLL(SYS_ArgumentTextRel sa)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(string.Format(@" 		SELECT  Paragraph.SDP_Content  as ATR_Content,
        TestCase.TCaseCode as CaseCode
FROM    [Ldt_JudgeSource].dbo.SD_CommonDoc_Paragraph AS Paragraph
        LEFT JOIN [Ldt_BatchIntelligence].dbo.LogicalParagraph AS LogicalParagraph ON LogicalParagraph.SC_ParaID = Paragraph.SDP_ID
        LEFT JOIN [Ldt_BatchIntelligence].dbo.Folders_Files AS Files ON LogicalParagraph.FL_ID = Files.FL_ID
        LEFT JOIN [Ldt_BatchIntelligence].dbo.TestCase AS TestCase ON TestCase.TCaseID = Files.TCaseID
WHERE   SDP_ID IN (
        SELECT  t2.SDP_ID
        FROM    [dbo].[LogicalParagraph] AS t1
                LEFT JOIN [Ldt_JudgeSource].dbo.SD_CommonDoc_Paragraph AS t2 ON t2.SD_ID = t1.SD_ID
        WHERE   ( t1.LP_Code = 'MS_CR_DefendantSuggest'
                  OR t1.LP_Code = 'MS_CR_ArgumentFocus'
                )
                AND t2.SDP_Content IS NOT NULL )
        AND Paragraph.SDP_ParagraphCode = '{0}'
        AND TestCase.TCaseCause = '{1}'
        AND TestCase.CaseType = '{2}'
        AND Files.FL_BookTypeName = '{3}'   ", sa.ATR_ParagraphCode, sa.CauseId,sa.ATR_CaseType, sa.ATR_BookType));
            if (!string.IsNullOrEmpty(sa.ATR_CourtID))
            {
                strSql.Append(string.Format(" and TestCase.TCourtName='{0}' ", sa.ATR_CourtID));
            }
            if (!string.IsNullOrEmpty(sa.ATR_Content))
            {
                strSql.Append(string.Format(" and ( Paragraph.SDP_Content like'%{0}%' or TestCase.TCaseCode like'%{0}%' ) ", sa.ATR_Content));
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        

    }
}
